﻿

using System;
using System.Drawing;

namespace _11_自定义控件
{
    [Serializable]//可序列化声明(允许此类创建出来的对象从内存条保存到硬盘上)
    public class Circle : Shape
    {

        /// <summary>
        /// 半径
        /// </summary>
        public int Radius { get; set; }

        /// <summary>
        /// 中心点
        /// </summary>
        public Point centPoint { get; set; }

        public Circle(string name, Color color,Point centerPoint) : base(name, color)
        {
            this.centPoint = centerPoint;
        }

        public override void Draw(Graphics g)
        {
            using (Pen pen = new Pen(Color, 2))
            {
                // 验证数据
                if (Radius > 0)
                {
                    // 绘制圆
                    g.DrawEllipse(pen, centPoint.X - Radius, centPoint.Y - Radius, 2 * Radius, 2 * Radius);
                }
            }
        }

        public override void Upadate(Point currentPoint)
        {
            //换一种编写的形式
            Radius = (int)Math.Sqrt(Math.Pow(currentPoint.X - centPoint.X, 2) + Math.Pow(currentPoint.Y - centPoint.Y, 2));

            //轨迹坐标点清空
            TrajectoryPoint.Clear();

            //将圆分为36端
            int segments = 36;

            for (int i = 0; i <= segments; i++)
            {
                //计算圆上的点
                int x = (int)(centPoint.X + Radius * Math.Cos(2 * Math.PI / segments * i));
                int y = (int)(centPoint.Y + Radius * Math.Sin(2 * Math.PI / segments * i));
                //添加到轨迹坐标点集合中
                TrajectoryPoint.Add(new Point(x, y));
            }


        }


    }
}
